<%@ page import="java.io.File"%>
<%@ page import="java.util.Date"%>
<%@ page import="java.text.SimpleDateFormat"%>
<%@ page import="org.apache.commons.codec.binary.Base64"%>

<!DOCTYPE html>

<%
  File currFile = new File(request.getSession().getServletContext().getRealPath(request.getServletPath()));
  String encodedPath = "";
  Base64 base64 = new Base64();
  if (currFile.getParentFile() != null) {
      String dirPath = currFile.getParentFile().getCanonicalPath();
      // encodedPath = Base64.getEncoder().encodeToString(dirPath.getBytes("UTF-8"));  // use jdk 1.8 and faster
      encodedPath = base64.encodeBase64String(dirPath.getBytes("UTF-8"));
  }

  StringBuilder driveInfo = new StringBuilder();
  File[] roots = File.listRoots();
  if (roots != null) {
      driveInfo.append("<ul>");
      for (File root : roots) {
          String rootPath = root.getCanonicalPath();
          long freeSpace = root.getFreeSpace();
				  long totalSpace = root.getTotalSpace();

          driveInfo.append("<li>");
          driveInfo.append("<a href=\"javascript:browseDir('").append(base64.encodeBase64String(rootPath.getBytes("UTF-8"))).append("');\">");
          driveInfo.append(root.getCanonicalPath());
          driveInfo.append("</a>");
          driveInfo.append("&nbsp;&nbsp;&nbsp;&nbsp;");
          driveInfo.append(String.format("%.2f", freeSpace / 1024.0 / 1024.0 / 1024.0)).append(" / ");
          driveInfo.append(String.format("%.2f", totalSpace / 1024.0 / 1024.0 / 1024.0)).append(" (GB)");
          driveInfo.append("</li>");
      }
      driveInfo.append("</ul>");
  }
%>

<html>
<head>
<title>Welcome to MINER</title>
<link type="text/css" rel="stylesheet" href="default.css"/>
<script type="text/javascript" src="jquery.min.js"></script>
</head>

<body>
<div id="container">
  <div id="content">
    <table class="form">
      <colgroup>
        <col width="18%"/>
        <col width="32%"/>
        <col width="18%"/>
        <col width="32%"/>
      </colgroup>
      <tr>
        <td class="fld" colspan="4"><b>System Information</b></td>
      </tr>
      <tr>
        <td class="fld">Server Name:</td>
        <td class="val"><%=java.net.InetAddress.getLocalHost().getHostName()%></td>
        <td class="fld">Date:</td>
        <td class="val"><%=new SimpleDateFormat("EEEE, MM/dd/yyyy").format(new Date())%></td>
      </tr>
      <tr>
        <td class="fld">OS Name:</td>
        <td class="val"><%=System.getProperty("os.name")%></td>
        <td class="fld">Java:</td>
        <td class="val"><%=System.getProperty("java.version")%> (<%=System.getProperty("java.vendor")%>)</td>
      </tr>
      <tr>
        <td class="fld">Available processors (cores):</td>
        <td class="val"><%=Runtime.getRuntime().availableProcessors()%></td>
        <td class="fld">Memory (MB):</td>
        <td class="val"><%=String.format("%.2f", Runtime.getRuntime().freeMemory() / 1024.0 / 1024.0)%> / <%=String.format("%.2f", Runtime.getRuntime().maxMemory() / 1024.0 / 1024.0)%> / <%=String.format("%.2f", Runtime.getRuntime().totalMemory() / 1024.0 / 1024.0)%> (Free / Total / JVM)</td>
      </tr>
      <tr>
        <td class="fld">Drive(s):</td>
        <td class="val" colspan="3"><%=driveInfo.toString()%></td>
      </tr>
    </table>

    <div class="info-tab">
      <ul>
        <li class="current">File Browsing</li>
        <li>File Upload</li>
      </ul>
    </div>

    <div id="browsingDiv"></div>

    <div id="uploadDiv" style="display:none;">
    <form id="uploadForm" action="uploadHandler.jsp" method="post" enctype="multipart/form-data">
      <div>Upload to Path: <input type="text" name="uploadPath" size="50"/> <input type="file" name="file" size="50"/></div>
      <div><input type="button" onclick="javascript:uploadFile();" value="Upload File"/></div>
    </form>
    </div>

  </div>

  <div id="footer">
    <div>&copy; 2015 MINER &trade;, All Rights Reserved.</div>
  </div>
</div>


<script type="text/javascript">
$(document).ready(function() {
    browseDir('<%=encodedPath%>');

    $('.info-tab li').click(function() {
        if (!$(this).hasClass('current')) {
            $('.info-tab li.current').removeClass('current');
            $(this).addClass('current');
            var idx = $(this).index();
            if (idx == 0) { // file browsing
                $('#browsingDiv').show();
                $('#uploadDiv').hide();
            } else if (idx == 1) {  // file upload
                $('#browsingDiv').hide();
                $('#uploadDiv').show();
            }
        }
    });
});

function browseDir(path) {
    if (path == null || path == '')
        return;

    $.ajax({
        url: 'browseHandler.jsp',
        data: {'path': path},
        type: 'POST',
        success: function(res) {
            $('#browsingDiv').html(res);
        }
    });
}

function uploadFile() {
    $('#uploadForm').submit();
}
</script>
</body>
</html>